package com.javasm.finance.service.impl;

import com.javasm.finance.dao.MenuDao;
import com.javasm.finance.dao.impl.MenuDaoImpl;
import com.javasm.finance.entity.Menu;
import com.javasm.finance.service.MenuService;
import com.javasm.finance.util.DBUtils;
import org.apache.commons.dbutils.DbUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MenuServiceImpl implements MenuService {
    MenuDao menuDao=new MenuDaoImpl();
    @Override
    public List<Menu> findMenu(Integer userId) {
        //一级菜单
        List<Menu> menuList=null;
        //二级菜单
        List<Menu> subMenuList=null;
        try {
            menuList= menuDao.findMenu(userId, 1);
            subMenuList=menuDao.findMenu(userId, 2);
            for (Menu menu : menuList) {
                for (Menu subMenu : subMenuList) {
                    if (menu.getMenuId().equals(subMenu.getParentId())){
                        if (menu.getSubMenu()==null){
                            menu.setSubMenu(new ArrayList<>());
                        }
                        menu.getSubMenu().add(subMenu);
                    }
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return menuList;
    }

    @Override
    public List<Menu> findAllMenu(Integer page, Integer pageSize,String menuName,Integer parentId) {
        List<Menu> menuList=null;
        try {
            menuList=menuDao.findAllMenu(page,pageSize,menuName,parentId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return menuList;
    }

    @Override
    public long findTotalNum(String menuName,Integer parentId) {
        long totalNum=0;
        try {
            totalNum=menuDao.findTotalNum(menuName,parentId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return totalNum;
    }

    @Override
    public List<Menu> findAllOneMenu() {
        List<Menu> oneMenuList = null;
        try {
            oneMenuList = menuDao.findAllOneMenu();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return oneMenuList;
    }

    @Override
    public int addMenu(Menu menu) {
        int rows = 0;
        try {
            rows = menuDao.addMenu(menu);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return rows;
    }

    @Override
    public Menu queryByMenuId(Integer menuId) {
        Menu menu = null;
        try {
            menu = menuDao.queryByMenuId(menuId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return menu;
    }

    @Override
    public int editMenu(Menu menu) {
        int rows = 0;
        try {
            rows = menuDao.editMenu(menu);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return rows;
    }

    @Override
    public boolean deleteMenu(Integer menuId) {
        boolean isSuccess = false;
        Connection conn = DBUtils.getConn();
        try {
            //关闭事务自动提交
            conn.setAutoCommit(false);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            //删除当条菜单数据(二级菜单数据)
            menuDao.deleteMenu(menuId, conn);
            //删除对应的二级菜单数据
            List<Menu> menuList = menuDao.querySecondMenu(menuId);
            if (menuList != null && menuList.size() > 0) {
                menuDao.deleteByParentId(menuId, conn);
            }
            //提交事务
            DbUtils.commitAndCloseQuietly(conn);
            isSuccess = true;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            //回退事务
            DbUtils.rollbackAndCloseQuietly(conn);
        }
        return isSuccess;
    }
}
